package com.youkeyi.ddy.cloud.basic.pub.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.pub.auth.entity.SysAuth;
import com.youkeyi.ddy.cloud.basic.pub.auth.service.SysAuthService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.PublicAuthCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.dto.PublicAuthListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.dto.PublicAuthListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.param.PublicAuthCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.param.PublicAuthDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.param.PublicAuthQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.auth.param.PublicAuthUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 权限（按钮）表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-02-19
 */
@Service
public class SysAuthController implements PublicAuthCloudService {

    @Autowired
    private SysAuthService sysAuthService;

    @Override
    public PublicAuthListCloudDto query(PublicAuthQueryCloudParam param) {
        PublicAuthListCloudDto dto = new PublicAuthListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getAuthPrimaryKeys())) {
            param.getAuthPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCodes())) {
            param.getCodes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getNames())) {
            param.getNames().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getAuthTypes())) {
            param.getAuthTypes().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysAuth> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getAuthPrimaryKeys()), SysAuth::getPrimaryKey, param.getAuthPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getNames()), SysAuth::getName, param.getNames())
                .like(StringUtils.isNotBlank(param.getNameFuzzy()), SysAuth::getName, param.getNameFuzzy())
                .in(CollectionUtils.isNotEmpty(param.getCodes()), SysAuth::getCode, param.getCodes())
                .like(StringUtils.isNotBlank(param.getUrlFuzzy()), SysAuth::getUrl, param.getUrlFuzzy())
                .in(CollectionUtils.isNotEmpty(param.getAuthTypes()), SysAuth::getAuthType, param.getAuthTypes())
                .ne(SysAuth::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysAuth> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysAuth> authList = sysAuthService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(authList.getRecords())) {
            dto.setTotal(authList.getTotal());

            List<PublicAuthListItemCloudDto> items = new ArrayList<>();
            authList.getRecords().forEach(auth -> {
                PublicAuthListItemCloudDto item = new PublicAuthListItemCloudDto();
                item.setAuthPrimaryKey(auth.getPrimaryKey());
                item.setAuthType(auth.getAuthType());
                item.setUrl(auth.getUrl());
                item.setIsDeleted(auth.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setName(auth.getName());
                item.setCode(auth.getCode());
                item.setCreateTime(auth.getCreateTime() != null ? auth.getCreateTime().getTime() : null);
                item.setCreateUserKey(auth.getCreateUserKey());
                item.setCreateUserName(auth.getCreateUserName());
                item.setUpdateTime(auth.getUpdateTime() != null ? auth.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(auth.getUpdateUserKey());
                item.setUpdateUserName(auth.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(PublicAuthCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getAuthType()) || StringUtils.isBlank(param.getUrl())
                || StringUtils.isBlank(param.getName()) || StringUtils.isBlank(param.getCode())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysAuth auth = new SysAuth();
        auth.setPrimaryKey(SimpleUUidUtils.getUuid());
        auth.setName(param.getName());
        auth.setCode(StringUtils.isNotBlank(param.getCode()) ? param.getCode() : String.format("AUTH-%s", System.currentTimeMillis()));
        auth.setAuthType(param.getAuthType());
        auth.setUrl(param.getUrl());
        auth.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        auth.setCreateUserKey(param.getCreateUserKey());
        auth.setCreateUserName(param.getCreateUserName());
        auth.setCreateTime(new Date());
        boolean save = sysAuthService.save(auth);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(auth.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(PublicAuthUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getAuthPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysAuth> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysAuth::getPrimaryKey, param.getAuthPrimaryKey());
        SysAuth auth = new SysAuth();
        auth.setPrimaryKey(param.getAuthPrimaryKey());
        auth.setName(param.getName());
        auth.setCode(param.getCode());
        auth.setAuthType(param.getAuthType());
        auth.setUrl(param.getUrl());
        auth.setUpdateUserKey(param.getUpdateUserKey());
        auth.setUpdateUserName(param.getUpdateUserName());
        auth.setUpdateTime(new Date());
        boolean save = sysAuthService.update(auth, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(PublicAuthDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getAuthPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysAuth> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysAuth::getPrimaryKey, param.getAuthPrimaryKeys());

        SysAuth auth = new SysAuth();
        auth.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        auth.setUpdateUserKey(param.getUpdateUserKey());
        auth.setUpdateUserName(param.getUpdateUserName());
        auth.setUpdateTime(new Date());
        boolean save = sysAuthService.update(auth, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
